package com.winter.common;

import org.apache.commons.lang3.StringUtils;

import java.io.File;

public class FileUtils {

    /**
     * 文件后缀限制
     */
    // 压缩包
    public static final String[] SUPPORTED_COMPRESS_PACKAGE_TYPE = { ".rar", ".zip" };
    // 图片
    public static final String[] SUPPORTED_IMAGE_TYPE = { ".jpg", ".jpeg", ".png", ".bmp" };

    /**
     * 文件名后缀是否支持
     */
    public static boolean isSupportedType(String fileName, String[] types) {
        if (StringUtils.isNotEmpty(fileName)) {
            for (String fileType : types) {
                if (fileName.endsWith(fileType)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 创建目录
     */
    public static boolean creatDir(String aDir) {
        File aFile = new File(aDir);
        if (!aFile.exists()) {
            return aFile.mkdirs();
        }
        return true;
    }

    /**
     * 是否超过限制的文件大小
     * 单位 byte
     */
    public static boolean isNotOverLimited(long cur, long limit) {

        return cur > limit;
    }

    /**
     * 文件大小 枚举
     */
    public enum FileSizeUnit {

        bs {
            public long toBs(long d) {
                return d;
            }

            public long toKbs(long d) {
                return d / (kb / b);
            }

            public long toMbs(long d) {
                return d / (mb / b);
            }

            public long toGbs(long d) {
                return d / (mb / b);
            }

            public long convert(long d, FileSizeUnit u) {
                return u.toBs(d);
            }
        },
        kbs {
            public long toBs(long d) {
                return x(d, kb / b, Long.MAX_VALUE / (kb / b));
            }

            public long toKbs(long d) {
                return d;
            }

            public long toMbs(long d) {
                return d / (mb / kb);
            }

            public long toGbs(long d) {
                return d / (gb / kb);
            }

            public long convert(long d, FileSizeUnit u) {
                return u.toKbs(d);
            }
        },
        mbs {
            public long toBs(long d) {
                return x(d, mb / b, Long.MAX_VALUE / (mb / b));
            }

            public long toKbs(long d) {
                return x(d, mb / kb, Long.MAX_VALUE / (mb / kb));
            }

            public long toMbs(long d) {
                return d;
            }

            public long toGbs(long d) {
                return d / (gb / mb);
            }

            public long convert(long d, FileSizeUnit u) {
                return u.toMbs(d);
            }
        },
        gbs {
            public long toBs(long d) {
                return x(d, gb / b, Long.MAX_VALUE / (gb / b));
            }

            public long toKbs(long d) {
                return x(d, gb / kb, Long.MAX_VALUE / (gb / kb));
            }

            public long toMbs(long d) {
                return x(d, gb / mb, Long.MAX_VALUE / (gb / mb));
            }

            public long toGbs(long d) {
                return d;
            }

            public long convert(long d, FileSizeUnit u) {
                return u.toMbs(d);
            }
        };

        static long x(long d, long m, long over) {
            if (d > over)
                return Long.MAX_VALUE;
            if (d < -over)
                return Long.MIN_VALUE;
            return d * m;
        }

        public long convert(long sourceDuration, FileSizeUnit unit) {
            throw new AbstractMethodError();
        }

        public long toBs(long duration) {
            throw new AbstractMethodError();
        }

        public long toKbs(long duration) {
            throw new AbstractMethodError();
        }

        public long toMbs(long duration) {
            throw new AbstractMethodError();
        }

        public long toGbs(long duration) {
            throw new AbstractMethodError();
        }

        private static final long b = 1l;
        private static final long kb = 1024 * b;
        private static final long mb = 1024 * kb;
        private static final long gb = 1024 * mb;
    }
}
